package idv.arthur.Servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class CallServlet01 {

	public static void main(String[] args) {
		InputStream ins = null;
		
		try {
			final URL gwtServlet = new URL(
					"http://10.80.71.72:8056/UtilWar/GetName?employeeNumber=842297");
			final HttpURLConnection servletConnection = (HttpURLConnection) gwtServlet.openConnection();
			ins = servletConnection.getInputStream();
			String res = convertStreamToString(ins);
			if ( res.equalsIgnoreCase("") ) {
				System.out.println("沒找到");	
			} else {
				System.out.println(res);
			}
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (ins != null) {
				try {
					ins.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	static public String convertStreamToString(InputStream is) throws IOException {
		if (is != null) {
			final Writer writer = new StringWriter();

			final char[] buffer = new char[1024];
			try {
				final Reader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
				int n;
				while ((n = reader.read(buffer)) != -1) {
					writer.write(buffer, 0, n);
				}
			} finally {
				is.close();
			}
			return writer.toString();
		} else {
			return "";
		}
	}
}
